package com.mapproject.industrymap.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mapproject.industrymap.entity.EstateInfoEntity;
import com.mapproject.industrymap.entity.SysUploadFileEntity;
import com.mapproject.industrymap.service.ShpTransforPgService;
import com.mapproject.industrymap.service.DxfTransforPgService;
import com.mapproject.industrymap.service.GeojsonTransforPgService;
import com.mapproject.industrymap.service.SysUploadFileService;
import com.mapproject.industrymap.utils.DataPgdump;
import com.mapproject.industrymap.utils.ReturnValue;
import com.mapproject.industrymap.utils.UnZipFile;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/postgres")
public class PostgresImpAndExpController {
    @Autowired
    SysUploadFileService sysUploadFileService;
    @Autowired
    ShpTransforPgService shpTransforPgService;
    @Autowired
    DxfTransforPgService dxfTransforPgService;
    @Autowired
    GeojsonTransforPgService jsonTransforPgService;

    /**
     * shp导入pg数据库，path为shp文件地址
     * @param path
     * @return
     */
    @RequestMapping(value = "/imp")
    public ReturnValue insertdata(@RequestParam(value = "path")String path){

        return new ReturnValue(200,"shp成功存入pg数据库",shpTransforPgService.insertShpData(path)) ;
    }
    //上传后存入pg库
    @RequestMapping(value = "/uploadimp")
    public ReturnValue uploadinsertdata(@RequestParam(value = "tablename")String tablename,@RequestParam(value = "filename")String filename){


        return new ReturnValue(200,"shp成功存入pg数据库",shpTransforPgService.uploadInsertShpData(tablename,filename)) ;
    }
    //dxf上传后存入pg库
    @RequestMapping(value = "/uploadimpdxf")
    public ReturnValue uploadinsertdxfdata(@RequestParam(value = "tablename")String tablename,@RequestParam(value = "filename")String filename){


        return new ReturnValue(200,"shp成功存入pg数据库",dxfTransforPgService.uploadInsertDxfData(tablename,filename)) ;
    }

    //geojson上传后存入pg库
    @RequestMapping(value = "/uploadimpjson")
    public ReturnValue uploadinsertjsondata(@RequestParam(value = "tablename")String tablename,@RequestParam(value = "filename")String filename){


        return new ReturnValue(200,"shp成功存入pg数据库",jsonTransforPgService.uploadInsertGeojsonData(tablename,filename)) ;
    }

    @RequestMapping(value = "/bigimp")
    public ReturnValue insertbigdata(@RequestParam("file") MultipartFile file,@RequestParam(value = "tablename",required = false)String tablename, @RequestParam(value = "rowguid")String rowguid){
        String fileName = file.getOriginalFilename();  // 文件名
        String suffixName = fileName.substring(fileName.lastIndexOf(".")+1);
        if(suffixName.equals("zip")){
            return shpTransforPgService.insertBigShpData(file,tablename,rowguid);
        }else {
            return new ReturnValue(500,"请上传zip文件");
        }

    }

}
